package com.p2p.front.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.p2p.base.exception.BizException;
import com.p2p.base.model.BaseUser;
import com.p2p.base.model.Money;
import com.p2p.base.page.PageVo;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.model.Tender;
import com.p2p.front.facade.UserTenderFacade;
import com.p2p.front.result.UserTenderResult;
import com.p2p.front.service.TenderReturnService;
import com.p2p.front.service.TenderService;

/**
 * @author ruiqi
 *
 */
@RequestMapping("/tender")
@Controller
public class UserTenderController extends BaseController {

	@Resource
	private UserTenderFacade userTenderFacade;

	@Resource
	private TenderService tenderService;

	@Resource
	private TenderReturnService tenderReturnService;

	@RequestMapping("/tenderManual")
	public String tenderManual(@RequestParam("tenderAmount") Long tenderAmount,
			Long loanId, ModelMap map) {
		/*
		 * if(tenderAmount==null){ map.addAttribute("loanId", loanId); return
		 * "/tender/tenderIndex"; }
		 */

		BaseUser user = getCurrentUser();

		Money amount = new Money(tenderAmount, 0);

		UserTenderResult userTenderResult = userTenderFacade.doTenderBiz(
				user.getId(), loanId, amount, 0, null);

		if (userTenderResult.isSuccess()) {
			Money finnalTenderAmount = userTenderResult.getTenderAmount();
			if (!finnalTenderAmount.equals(amount)) {
				amount = finnalTenderAmount;
			}
		} else {
			String error_code = userTenderResult.getResultCode();
			map.addAttribute("error_code", error_code);
			String msg = BizException.getGlobalMessage(error_code);
			if(StringUtils.isNotBlank(msg)){
				map.addAttribute("error_code", msg);
			}
			return "errorPage";
		}

		map.addAttribute("succeseAmount", amount.getYuan());
		map.addAttribute("loanId", loanId);
		return "/tender/tenderResult";

	}

	@RequestMapping("/myLoanList")
	public String myLoanList(PageVo form, Model model) {
		BaseUser user = getCurrentUser();
		form.getParams().put("uid", user.getId());
		PageVo page = tenderService.myLoanList(form);
		model.addAttribute("page", page);
		return "tender/my_loan_list";
	}

	// 等待放款的不会太多，不分页
	@RequestMapping("/myLoanWaitList")
	public String myLoanWaitList(Model model) {
		BaseUser user = getCurrentUser();
		PageVo page = new PageVo();
		List<Map<String, Object>> list = tenderService.myWaitLoanList(user
				.getId());
		page.setList(list);
		page.setPageNum(1);
		int size = 0;
		int pages = 0;
		if (list != null && list.size() > 0) {
			size = list.size();
			pages = 1;
		}

		page.setTotalCount(size);
		page.setPages(pages);
		model.addAttribute("page", page);
		return "tender/my_loan_wait_list";
	}

	@RequestMapping("/myLoanTenderingList")
	public String myLoanTenderingList(PageVo form, Model model) {
		BaseUser user = getCurrentUser();
		// 普通标才会出现11投标成功，即招标中
		form.getParams().put("status", "11");
		form.getParams().put("uid", user.getId());
		PageVo page = tenderService.myLoanList(form);
		model.addAttribute("page", page);
		return "tender/my_loan_tendering_list";
	}

	@RequestMapping("/myRecyclingTenderReturnList")
	public String recyclingTenderReturn(PageVo form, ModelMap map) {
		BaseUser user = getCurrentUser();
		form.getParams().put("status", "21");
		form.getParams().put("uid", user.getId());
		form.handleDatetimeDate();
		PageVo page = tenderReturnService.listPage(form);
		form.resetDate();
		map.addAttribute("page", page);
		return "tender/my_recycling_tender_return_list";
	}

	@RequestMapping("/myRecycledTenderReturnList")
	public String recycledTenderReturn(PageVo form, ModelMap map) {
		BaseUser user = getCurrentUser();
		form.getParams().put("status", "31");
		form.getParams().put("uid", user.getId());
		PageVo page = tenderReturnService.listPageRecycled(form);
		map.addAttribute("page", page);
		return "tender/my_recycled_tender_return_list";
	}

}
